//
//  CommentSampleVC.swift
//  BaseSwift
//
//  Created by gh on 2019/10/14.
//  Copyright © 2019 ghwang. All rights reserved.
//

import UIKit

class CommentSampleVC: UIViewController {

    var page = 1
    var pid:String?
    var type:String?
    var dataArray:Array<CommentVo> = Array()
    
    func sendRequest(){
        
        if self.type! == "project" {
            
            API.projectCommentList(params: ["page":page.description,
                                            "projectId":self.pid!]) { (result) in
                
                                                let page = result as! Page<CommentVo>
                                                self.handleResult(pageVo: page)
            }
        }else if self.type! == "product"{
            
            
            API.productCommentList(params: ["page":page.description,
                                            "pdtId":self.pid!]) { (result) in
                
                                                
                                                let page = result as! Page<CommentVo>
                                                self.handleResult(pageVo: page)
            }
        }else if self.type! == "line"{
            
            API.projectCommentList(params: ["page":page.description,
                                            "projectId":self.pid!]) { (result) in
                
                                                let page = result as! Page<CommentVo>
                                                self.handleResult(pageVo: page)
            }
        }
        
    }
    func handleResult(pageVo:Page<CommentVo>){
        
        self.tableView.mj_header.endRefreshing()
        self.tableView.mj_footer.endRefreshing()
        
        if page == 1 {
            
            self.dataArray = pageVo.list
            
        }else{
            
            for item in pageVo.list {
                
                self.dataArray.append(item)
            }
        }
        
        
        if pageVo.list.count < 20{
            
            self.tableView.mj_footer.endRefreshingWithNoMoreData()
        }
        self.tableView.reloadData()
        
    }

    lazy var commentBottomView:UIView = {
        
        let bg = UIView()

        bg.backgroundColor = UIColor.white
        
        let commBtnBg = UIButton()
        commBtnBg.sd_cornerRadius = 16
        bg.addSubview(commBtnBg)
        commBtnBg.addTarget(self, action: #selector(showCommentAlert), for: .touchUpInside)
        commBtnBg.backgroundColor = UIColor.backgroundColor
        let _ = commBtnBg.sd_layout()?.rightSpaceToView(bg,16)!.heightIs(32)!.leftSpaceToView(bg,16)?.centerYEqualToView(bg)

        
        let lab = UILabel()
        lab.font = UIFont.commonFontSmaller
        lab.textColor = UIColor.tabbarDefaultTextColor
        lab.text = "说点啥..."
        commBtnBg.addSubview(lab)
        let _ = lab.sd_layout()?.centerXEqualToView(bg)!.heightIs(20)!.centerYEqualToView(commBtnBg)!.widthIs(135)
        
        return bg
        
    }()
    
    lazy var tableView:UITableView = {
        
        
        var tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.backgroundColor = UIColor.clear
        tableView.tableFooterView = UIView()
        tableView.separatorColor = UIColor.clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(UINib.init(nibName: "CommentSampleCell", bundle: nil), forCellReuseIdentifier: "CommentSampleCell")
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.navigationView.setTitle("评论")
        self.view.backgroundColor = UIColor.backgroundColor
        self.navigationView.removeAllLeftButton()
        self.navigationView.addLeftButton(with: UIImage(named: "关闭")) { (view) in
            
            self.dismiss(animated: true) {
                
            }
        }
        
        self.view.addSubview(tableView)
        let _ = tableView.sd_layout()?.spaceToSuperView(UIEdgeInsets(top: HT_StatusBarAndNavigationBarHeight, left: 0, bottom: HT_TabbarSafeBottomMargin+45, right: 0))
        
        
        let header = MJRefreshNormalHeader.init {
            
            self.page = 1
            self.sendRequest()
        }
        header?.stateLabel.isHidden = true
        header?.lastUpdatedTimeLabel.isHidden = true
        self.tableView.mj_header = header
        
        let footer = MJRefreshAutoNormalFooter.init {

            self.page += 1
            self.sendRequest()
        }
        self.tableView.mj_footer = footer
        
        if self.type! == "project"{
            
                    let _ = tableView.sd_layout()?.spaceToSuperView(UIEdgeInsets(top: HT_StatusBarAndNavigationBarHeight, left: 0, bottom: HT_TabbarSafeBottomMargin, right: 0))
            
        }else{
            
            self.view.addSubview(self.commentBottomView)
            let _ = commentBottomView.sd_layout()?.widthRatioToView(view,1)!.heightIs(45)!.bottomSpaceToView(view,HT_TabbarSafeBottomMargin)
        }

        self.sendRequest()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    @objc func sendBtnClick(){
            
        
          if SPUtil.init().loginStatus == 0 {
              
              APP.backToVC = self
              PUSH(from: self, to: LoginViewController())
              return
          }
          if SPUtil.init().mobile.count == 0 {
              
              APP.backToVC = self
              PUSH(from: self, to: BindPhoneVC())
              return
        
          }
        
        if inputV.text.count == 0 {
            
            ToastUtil.showHUDWarn(msg: "随便写点什么吧")
            return
        }
        if self.type! == "line" {
            
            API.projectComment(params: [
                "pdtId":self.pid!,
                "mobile":SPUtil.init().mobile!,
                "content":inputV.text!]) { (result) in
                
                                            ToastUtil.show(msg: "评论已提交")
                                            self.commentbg.removeFromSuperview()
                    self.page = 1
                    self.sendRequest()
                                            

            }
            
            return
        }
        
        API.productComment(params: [
            "pdtId":self.pid!,
            "mobile":SPUtil.init().mobile!,
            "content":inputV.text!]) { (result) in
            
                                        ToastUtil.show(msg: "评论已提交")
                                        self.commentbg.removeFromSuperview()
                self.page = 1
                self.sendRequest()
                                        

        }
            
    }
    @objc func cancelBtnClick(){
            
        commentbg.removeFromSuperview()
    }
    
    let commentbg = UIView()
    let inputV = GCPlaceholderTextView()
    

    @objc func showCommentAlert() {
            
        self.view.addSubview(commentbg)
        let _  = commentbg.sd_layout()?.spaceToSuperView(.zero)

        let bgImgv = UIImageView()
        bgImgv.alpha = 0.5
        bgImgv.backgroundColor = UIColor.init(patternImage: UIImage(named: "paybg")!)
        commentbg.addSubview(bgImgv)
        let _ = bgImgv.sd_layout()?.spaceToSuperView(.zero)
        
        let bg2 = UIView()
        bg2.backgroundColor = UIColor.white
        commentbg.addSubview(bg2)
        let _ = bg2.sd_layout()?.bottomSpaceToView(commentbg,0)?.widthRatioToView(commentbg,1)?.heightIs(166)
        
        bg2.addSubview(inputV)
        inputV.placeholder = "发表评论"
        inputV.sd_cornerRadius = 3
        inputV.font = UIFont.commonFontSmall
        inputV.textColor = UIColor.textTitleColorBlack
        let _ = inputV.sd_layout()?.leftSpaceToView(bg2,16)!.rightSpaceToView(bg2,16)!.heightIs(90)!.topSpaceToView(bg2,25)
        inputV.layer.borderColor = UIColor.textTitleColorBlack.cgColor
        inputV.layer.borderWidth = 0.5
        
        let sendBtn = UIButton()
        sendBtn.setTitle("发送", for: .normal)
        sendBtn.addTarget(self, action: #selector(sendBtnClick), for: .touchUpInside)
        sendBtn.setTitleColor(UIColor.btnMainColor, for: .normal)
        sendBtn.titleLabel?.font = UIFont.commonFontSmall
        bg2.addSubview(sendBtn)
        
        let _ = sendBtn.sd_layout()?.rightSpaceToView(bg2,16)!.heightIs(28)!.widthIs(38)!.topSpaceToView(inputV,5)
        
        let cancelBtn = UIButton()
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)
        cancelBtn.setTitleColor(UIColor.GP_Red_Color, for: .normal)
        cancelBtn.titleLabel?.font = UIFont.commonFontSmall
        bg2.addSubview(cancelBtn)
        
        let _ = cancelBtn.sd_layout()?.leftSpaceToView(bg2,16)!.heightIs(28)!.widthIs(38)!.topSpaceToView(inputV,5)

            
        }

}
extension CommentSampleVC:UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 315-165
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "CommentSampleCell") as! CommentSampleCell
        cell.setModel(mod: self.dataArray.get(at: indexPath.row)!)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }
    

    
}
